// 网关配置的前缀
var jobsServer = "/jobsSer"

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量

// let CorHId = "400001";
let CorHId = "";
var search_value = '';

let Authorization = "";
let refreshToken = "";
// 加载页面时读取后台的 ip
window.onload = function () {
    let authorities = $("#authorities").html();
    unauthorized_2(authorities, "jobApproval");

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        jobsServer = data.jobsServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    CorHId = $("#userId").html();
    getHuntingStatus(currentPage, pageSize, search_value);
}

function getHuntingStatus(currentPage, pageSize, search_value) {

    $.ajax({
        type: "get",
        async: false,
        url: jobsServer + "/jobs/huntingInfo",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            currentPage: currentPage,
            pageSize: pageSize,
            searchValue: search_value,
            userId: CorHId
        },
        success: function (data) {
            console.log("所有的岗位申请状态数据 - 第 " + currentPage + " 页");
            total = data.datas.total;
            pageSize = data.datas.size;
            pages = data.datas.pages;

            //清空数据
            $("#huntingList").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {
                let status = "";
                if (item.huntingStatus === "邀面试") {
                    status = "<span class='rejected'>邀面试</span>";
                } else if (item.huntingStatus === "感兴趣") {
                    status = "<span class='approved'>感兴趣</span>";
                } else {
                    status = "<span class='pending'>" + item.huntingStatus + "</span>";
                }

                $("#huntingList").append(
                    "<tr class='application-item'>" +
                    "<td class='application-job'>" +
                    "<h3><a>" + (i + 1) + "</a></h3>" +
                    "</td>" +

                    "<td class='application-employer'>" +
                    "<a class='dotted' onclick='jobDetail(\""+item.jobId+"\", \""+item.companyId+"\")'>" + item.jobName + "</a>" +
                    "</td>" +

                    "<td class='application-created'>" +
                    "<span>" + item.userName + "</span>" +
                    "</td>" +

                    "<td class='download-cv'>" +
                    "<a  onclick='userDetail(\"" + item.userId + "\", \"" + item.huntingId + "\")'>" +
                    "<i class='lnr lnr-eye'></i> 在线查看</a>" +
                    "</td>" +

                    "<td class='application-created'>" +
                    "<span>" + item.createDate + "</span>" +
                    "</td>" +

                    "<td class='status'>" +
                    status +
                    "</td>" +

                    "<td class='application-created'>" +
                    "<span>" + item.hrName + "</span>" +
                    "</td>" +

                    "<td class='application-created'>" +
                    "<span>" + item.updateDate + "</span>" +
                    "</td>" +
                    "</tr>"
                );
            });

            // pageMe.js 使用方法
            $("#page").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getHuntingStatus(num, size, search_value);
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });

}

//  搜索框
function click_search() {
    var tmp = $('#approval_search').val();
    if (tmp === "" || tmp === null) {
        tmp = "";
    }
    search_value = tmp;

    getHuntingStatus(1, pageSize, search_value);
}

function userDetail(userId, huntingId) {
    location.href = "/oa/jobs/CandidateDetailsPage/" + userId + "/" + CorHId + "/" + huntingId;
}
function jobDetail(jobId, companyId){
    location.href ="/oa/jobs/jobDetails/"+jobId+"/"+companyId;
}
